package com.nondo.rdp.finance.endpoint.api.admin.setting.home;

import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.setting.home.SettingModule;
import com.nondo.rdp.finance.domain.setting.home.SettingModuleCustomer;
import com.nondo.rdp.finance.logic.SettingModule.SettingModuleLogic;
import com.nondo.rdp.finance.model.setting.home.SettingModuleCustomerBean;
import com.nondo.rdp.finance.service.setting.home.SettingModuleCustomerService;
import com.nondo.rdp.finance.service.setting.home.SettingModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController("apiAdminSettingHomeModuleCustomer")
@RequestMapping("/api/admin/setting/home/module/customer")
public class ModuleCustomerEndpoint {
    @Autowired
    SettingModuleService settingModuleService;
    @Autowired
    SettingModuleCustomerService settingModuleCustomerService;
    @Autowired
    SettingModuleLogic settingModuleLogic;

    /**
     * 获取设置模块自定义列表
     *
     * @param id
     * @return
     */
    @RequestMapping("/list/{id}")
    public View list(@PathVariable("id") long id) {
        View view = new View(20000, "获取列表成功");

        //查询模块是否存在
        SettingModule settingModule = this.settingModuleService.findOne(id);

        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModuleCustomer> list = this.settingModuleCustomerService.findAll(settingModule, sort);

        List newList = new ArrayList();
        for (SettingModuleCustomer item : list) {
            SettingModuleCustomerBean settingModuleCustomerBean = new SettingModuleCustomerBean();
            BeanUtils.copy(item, settingModuleCustomerBean);

            //处理附加数据
            settingModuleCustomerBean.setExtra(settingModuleLogic.getAdminTypeContent(item.getType(), item.getValue()));

            //处理图片
            settingModuleCustomerBean.setImageFull(CommUtil.getFileUrl(settingModuleCustomerBean.getImage()));
            settingModuleCustomerBean.setImage(item.getImage());

            newList.add(settingModuleCustomerBean);
        }

        view.setData(newList);

        return view;
    }

    /**
     * 添加模块自定义
     *
     * @param request
     * @param response
     * @param settingModuleCustomer
     * @param result
     * @return
     */
    @RequestMapping("/add/{id}")
    public View add(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") long id, @Validated @RequestBody SettingModuleCustomer settingModuleCustomer, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //判断自定义模块
            SettingModule settingModule = this.settingModuleService.findOne(id);
            if (settingModule == null) {
                return View.ofError("不存在的自定义模块，无法添加");
            }
            settingModuleCustomer.setModule(settingModule);

            //判断提交的更多类型是不是有效的
            boolean checkTypeValue = settingModuleLogic.checkTypeValue(settingModuleCustomer.getType(), settingModuleCustomer.getValue());
            if (!checkTypeValue) {
                return View.ofError("操作类型和值不匹配");
            }
            this.settingModuleCustomerService.save(settingModuleCustomer);
        }

        return view;
    }

    /**
     * 更新设置模块自定义
     *
     * @param request
     * @param response
     * @param settingModuleCustomerBean
     * @param result
     * @return
     */
    @RequestMapping("/update")
    public View update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody SettingModuleCustomerBean settingModuleCustomerBean, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //判断提交的更多类型是不是有效的
            boolean checkTypeValue = settingModuleLogic.checkTypeValue(settingModuleCustomerBean.getType(), settingModuleCustomerBean.getValue());
            if (!checkTypeValue) {
                return View.ofError("操作类型和值不匹配");
            }

            //数据拷贝
            SettingModuleCustomer settingModuleCustomer = new SettingModuleCustomer();
            BeanUtils.copy(settingModuleCustomerBean, settingModuleCustomer);

            this.settingModuleCustomerService.update(settingModuleCustomer);
        }

        return view;
    }

    /**
     * 删除自定义
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public View delete(@PathVariable("id") Long id) {
        this.settingModuleCustomerService.delete(id);

        return View.ofOk("删除成功");
    }
}